/**
 * Created by Administrator on 2017/8/10.
 */
$(document).ready(function () {
    if ($(window).validate) {
        //身份证验证
        $.validator.addMethod("ishenfid", function (value, element, param) {
            return this.optional(element) || /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(value);
        }, "请输入15位数字或18位数字或17位数字+x")

        //电话号码
        $.validator.addMethod("istelpho", function (value, element, param) {
            return this.optional(element) || /(^1(3|4|5|7|8)\d{9}$)/.test(value);
        }, "请输入正确电话号码")

        // //大于0
        // $.validator.addMethod("gtzero", function (value, element, param) {
        //     return this.optional(element) || (value > 0)
        // }, "请输入大于0的整数数字")

        $('.product_order').validate({
            onfocusout: function (element) {
                $(element).valid();
            },//修改
            onkeyup: false,//修改
            rules: {
                name: "required",
                phone: {
                    required: true,
                    istelpho: true
                },
                shenfz: {
                    required: true,
                    ishenfid: true
                }
            },
            messages: {
                name: "请输入您的姓名",
                phone: {
                    required: "请输入手机号码",
                    istelpho: "请输入正确手机"
                },
                shenfz: {
                    required: "请输入身份证号码",
                    ishenfid: "请输入15或18位数字或17位数字+X"
                }
            },
            errorPlacement: function (error, element) {
                error.appendTo(element.parent());
            }
        })
    }
    ;

    //景区预订数量加减
    //加
    $(".main_box .yd_iont .numll_odd").on("click", function () {
        var inputnum = parseInt($(this).siblings(".numll").val()) + 1;
        $(this).siblings(".numll").val(inputnum);
        //景区数量加减数据后生成用户信息表单
        var needdom = inputnum - $(".main_box .user_iont .iont_info").length;
        var htmldom = "";
        for (var i = 0; i < needdom; i++) {
            var inum = $(".main_box .user_iont .iont_info").length + i + 1;
            htmldom += '<div class="iont_info">';
            htmldom += '<div class="iont_list user_name">';
            htmldom += '<label>取票人' + inum + '：</label>';
            htmldom += '<span class="red">*</span>';
            htmldom += '<em class="form_contion">';
            htmldom += '<input name="name' + inum + '" type="text"  class="user_name" placeholder="请填写出游人">';
            htmldom += '</em>';
            htmldom += '</div>';
            htmldom += '<div class="iont_list user_phone">';
            htmldom += '<label>手机号码' + inum + '：</label>';
            htmldom += '<span class="red">*</span>';
            htmldom += '<em class="form_contion">'
            htmldom += '<input name="phone' + inum + '" type="text" class="user_name" placeholder="请填写联系人方式"><small>（免费接收订单确认短信）</small>';
            htmldom += '</em>';
            htmldom += '</div>';
            htmldom += '<div class="iont_list user_id">';
            htmldom += '<label>身份证号' + inum + '：</label>';
            htmldom += '<span class="red">*</span>';
            htmldom += '<em class="form_contion">'
            htmldom += '<input name="shenfz' + inum + '" type="text" class="user_name"  placeholder="请填写证件号">';
            htmldom += '</em>';
            htmldom += '</div>';
            htmldom += '</div>';
        }
        $(".main_box .user_iont").append(htmldom);

        //用户名
        $(".main_box .user_iont .user_name input[type='text']").each(function () {
            $(this).rules("add", {required: true, messages: {required: "请输入姓名"}});
        })
        //手机号码
        $(".main_box .user_iont .user_phone  input[type='text']").each(function () {
            $(this).rules("add", {
                required: true,
                istelpho: true,
                messages: {required: "请输入手机号码", istelpho: "请输入正确手机"}
            });
        })
        //身份证号码
        $(".main_box .user_iont .user_id  input[type='text']").each(function () {
            $(this).rules("add", {
                required: true,
                ishenfid: true,
                messages: {required: "请输入身份证号码", istelpho: "请输入15或18位数字或17位数字+X"}
            });
        })
    })
    //减
    $(".main_box .yd_iont .numll_reduce").on("click", function () {
        var inputnum = parseInt($(this).siblings(".numll").val()) - 1;
        if (inputnum < 1) {
            return
        }
        $(this).siblings(".numll").val(inputnum);

        //景区数量加减数据后生成用户信息表单
        $(".main_box .user_iont .iont_info:last").remove();
        //用户名
        $(".main_box .user_iont .user_name input[type='text']").each(function () {
            $(this).rules("add", {required: true, messages: {required: "请输入姓名"}});
        })
        //手机号码
        $(".main_box .user_iont .user_phone input[type='text']").each(function () {
            $(this).rules("add", {
                required: true,
                istelpho: true,
                messages: {required: "请输入手机号码", istelpho: "请输入正确手机"}
            });
        })
        //身份证号码
        $(".main_box .user_iont .user_id  input[type='text']").each(function () {
            $(this).rules("add", {
                required: true,
                ishenfid: true,
                messages: {required: "请输入身份证号码", istelpho: "请输入15或18位数字或17位数字+X"}
            });
        })
    })
    //输入数量发生变化
    $(".main_box .yd_iont .numll").on("input", function () {
        if (isNaN($(this).val()) || $(this).val() <= 0) {
            $(this).val(1);
        }
        var inputnum = parseInt($(this).val());

        //景区数量加减数据后生成用户信息表单
        var htmldom = "";
        for (var i = 0; i < inputnum; i++) {
            var inum = i + 1;
            htmldom += '<div class="iont_info">';
            htmldom += '<div class="iont_list user_name">';
            htmldom += '<label>取票人' + inum + '：</label>';
            htmldom += '<span class="red">*</span>';
            htmldom += '<em class="form_contion">';
            htmldom += '<input name="name' + inum + '" type="text"  class="user_name" placeholder="请填写出游人">';
            htmldom += '</em>';
            htmldom += '</div>';
            htmldom += '<div class="iont_list user_phone">';
            htmldom += '<label>手机号码' + inum + '：</label>';
            htmldom += '<span class="red">*</span>';
            htmldom += '<em class="form_contion">'
            htmldom += '<input name="phone' + inum + '" type="text" class="user_name" placeholder="请填写联系人方式"><small>（免费接收订单确认短信）</small>';
            htmldom += '</em>';
            htmldom += '</div>';
            htmldom += '<div class="iont_list user_id">';
            htmldom += '<label>身份证号' + inum + '：</label>';
            htmldom += '<span class="red">*</span>';
            htmldom += '<em class="form_contion">'
            htmldom += '<input name="shenfz' + inum + '" type="text" class="user_name"  placeholder="请填写证件号">';
            htmldom += '</em>';
            htmldom += '</div>';
            htmldom += '</div>';
        }
        $(".main_box .user_iont").html(htmldom);

        //用户名
        $(".main_box .user_iont .user_name input[type='text']").each(function () {
            $(this).rules("add", {required: true, messages: {required: "请输入姓名"}});
        })
        //手机号码
        $(".main_box .user_iont .user_phone   input[type='text']").each(function () {
            $(this).rules("add", {
                required: true,
                istelpho: true,
                messages: {required: "请输入手机号码", istelpho: "请输入正确手机"}
            });
        })
        //身份证号码
        $(".main_box .user_iont .user_id  input[type='text']").each(function () {
            $(this).rules("add", {
                required: true,
                ishenfid: true,
                messages: {required: "请输入身份证号码", istelpho: "请输入15或18位数字或17位数字+X"}
            });
        })
    })
    var totalPrice = $('#total-price')
    var number = $('.yd_iont .numll')
    var singlePrice = totalPrice.text()

    $('.yd_iont').on('click','.numll_odd',function(){
        totalPrice.text((number.val() * singlePrice).toFixed(2))
    })
    $('.yd_iont').on('click','.numll_reduce',function(){
        totalPrice.text((number.val() * singlePrice).toFixed(2))
    })
})

